<?php

require_once "config.class.php";

class Model
{
    public $db = NULL;
    public $soap = NULL;

    public function __construct () {
        $cfg = Config::getInstance();
        $db_config = $cfg->db;

        switch ($db_config['dbtype']) {
        case 'mssql':
            if (isset($this->db_name)) {
                require_once "mssqldb.class.php";
                $this->db = new MssqlDb($db_config['hostname'], 
                                        $db_config['username'], 
                                        $db_config['password']);
                $this->db->connect();
                $this->db->selectDb($this->db_name);

            }
            
            break;
        case 'pgsql':
            require_once "pgsqldb.class.php";
            $this->db = new PgsqlDb($db_config['connstr']);
            $this->db->connect();
            break;

        case 'mysql':
            require_once "mysqldb.class.php";
            $this->db = new MysqlDb($db_config['hostname'],
                                    $db_config['username'],
                                    $db_config['password']);
            $this->db->connect();
            $this->db->selectDb($this->db_name);

            break;
        default:
            throw new Exception("Wrong db type.");
        }



        if (isset($this->soap_url)) {
            require_once "nuSOAP/nusoap.php";
            $this->soap = new soapclient($this->soap_url, true);
            $this->soap->soap_defencoding = 'utf-8';
            $this->soap->xml_encoding = 'utf-8';
            $this->soap->decode_utf8 = false;
        }

    }
    public function __destruct () {
        if (isset($this->db)) {
            $this->db->close();
        }
    }
}


